//
//  CardView.m
//  MyWallet
//
//  Created by 小肥羊 on 15/7/15.
//  Copyright (c) 2015年 小肥羊. All rights reserved.
//

#import "CardView.h"

@implementation CardView

#pragma mark - 初始化
- (void)awakeFromNib
{
    [[NSBundle mainBundle] loadNibNamed:@"CardView" owner:self options:nil];
    self.contentView.frame = self.bounds;
    [self addSubview:self.contentView];
    [self.layer setCornerRadius:kNomalRadius];
    [self.layer setMasksToBounds:YES];
    self.isActivation = NO;
    
    //上滑
    UISwipeGestureRecognizer *recognizerUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(drawMoney:)];
    recognizerUp.direction = UISwipeGestureRecognizerDirectionUp;
    [self addGestureRecognizer:recognizerUp];
    //下滑
    UISwipeGestureRecognizer *recognizerDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(saveMoney:)];
    recognizerDown.direction = UISwipeGestureRecognizerDirectionDown;
    [self addGestureRecognizer:recognizerDown];
    //长按
    UILongPressGestureRecognizer * longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)];
    longPressGr.minimumPressDuration = 1.0;
    [self addGestureRecognizer:longPressGr];

}

//- (id)initWithCoder:(NSCoder *)aDecoder
//{
//    if (self = [super initWithCoder:aDecoder]) {
//        UIView *containerView = [[[UINib nibWithNibName:@"CustomView" bundle:nil] instantiateWithOwner:self options:nil] objectAtIndex:0];
//        CGRect newFrame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
//        containerView.frame = newFrame;
//        [self addSubview:containerView];
//    }
//    return self;
//}


#pragma mark - Set 设置
- (void)setIsActivation:(BOOL)isActivation
{
    _isActivation = isActivation;
    if (_isActivation) {
        [self.cardNameLabel setHidden:NO];
        [self.moneyLabel setHidden:NO];
        [self.activationButton setTitle:@"－" forState:UIControlStateNormal];
        [self.contentView setBackgroundColor:[UIColor whiteColor]];
    } else {
        [self.cardNameLabel setHidden:YES];
        [self.moneyLabel setHidden:YES];
        [self.activationButton setTitle:@"+" forState:UIControlStateNormal];
        [self.contentView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    }
}

#pragma mark - 触摸调用方法
/*! @brief 触摸开始时调用此方法 */
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.contentView setBackgroundColor:[UIColor lightGrayColor]];
}
/*! @brief 手离开屏幕，触摸事件结束 */
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (self.isActivation) {
        [self.contentView setBackgroundColor:[UIColor whiteColor]];
    } else {
        [self.contentView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    }
}
/*! @brief 手在界面上移动 */
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

}
/*! @brief 手离开界面 */
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (self.isActivation) {
        [self.contentView setBackgroundColor:[UIColor whiteColor]];
    } else {
        [self.contentView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    }
}

#pragma mark - 操作
- (void)saveMoney:(UISwipeGestureRecognizer *)tap
{
    if (self.delegate) {
        [self.delegate saveMoney:self];
    }
}

- (void)drawMoney:(UISwipeGestureRecognizer *)tap
{
    if (self.delegate) {
        [self.delegate drawMoney:self];
    }
}

-(void)longPressToDo:(UILongPressGestureRecognizer *)gesture
{
    if(gesture.state == UIGestureRecognizerStateBegan)
    {
        if (self.delegate) {
            [self.delegate checkCard:self];
        }
    }
}


- (IBAction)activation:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"－"]) {
        if (self.delegate) {
            [self.delegate cancellationCard:self];
        }
    } else if ([sender.titleLabel.text isEqualToString:@"+"]) {
        if (self.delegate) {
            [self.delegate activationCard:self];
        }
    }
}

#pragma mark - 公共方法
- (void)upCard:(CardModel *)card
{
    [self.cardNameLabel setText:card.cardName];
    [self.moneyLabel setText:[[NSString alloc] initWithFormat:@"$ %.2f",card.cardMoney]];
    self.isActivation = card.isActivation;
}
- (void)setMoney:(float)money;
{
    [self.moneyLabel setText:[[NSString alloc] initWithFormat:@"$ %.2f",money]];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
